package com.tj.bo.user.server;

import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.tj.bo.common.conf.util.ShareCodeUtils;
import com.tj.bo.user.client.UserPromotionService;
import com.tj.bo.user.client.vo.output.UserPromotionInfoRO;
import com.tj.bo.user.client.vo.output.UserPromotionInviteRecordRO;
import com.tj.bo.user.client.vo.output.UserPromotionMappingInfo;
import com.tj.bo.user.server.dao.UserAccessDao;
import com.tj.bo.user.server.dao.UserPromotionMappingInfoDao;
import com.tj.bo.user.server.dao.UserPromotionRecordDao;
import com.tj.bo.user.server.entity.UserAccessEntity;
import com.tj.bo.user.server.entity.UserPromotionMappingInfoEntity;
import com.tj.bo.user.server.entity.UserPromotionRecordEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: binary-option
 * @description
 * @author: frank
 * @create: 2020-08-21 11:45
 **/
@Service
public class UserPromotionServiceImpl implements UserPromotionService {

    @Autowired
    private UserPromotionMappingInfoDao userPromotionMappingInfoDao;
    @Autowired
    private UserPromotionRecordDao userPromotionRecordDao;
    @Autowired
    private UserAccessDao userAccessDao;

    @Override
    public UserPromotionInfoRO getUserPromotionByUserCode(String userCode) {
        UserPromotionMappingInfoEntity userPromotionMappingInfoEntity = userPromotionMappingInfoDao.queryUserPromotionInfoByUserCode(userCode);
        UserPromotionInfoRO userPromotionInfoRO = null;
        if(userPromotionMappingInfoEntity != null){
            userPromotionInfoRO = new UserPromotionInfoRO();
            BeanUtils.copyProperties(userPromotionMappingInfoEntity, userPromotionInfoRO);
        }
        return userPromotionInfoRO;
    }

    @Override
    public List<UserPromotionInviteRecordRO> getUserPromotionInviteRecordByUserCode(String userCode) {
        List<UserPromotionRecordEntity> userPromotionRecordEntities = userPromotionRecordDao.queryUserPromotionRecordByUserCode(userCode);
        List<UserPromotionInviteRecordRO> promotionInviteRecordROS = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(userPromotionRecordEntities)){
            for(UserPromotionRecordEntity entity : userPromotionRecordEntities){
                UserPromotionInviteRecordRO userPromotionInviteRecordRO = new UserPromotionInviteRecordRO();
                BeanUtils.copyProperties(entity, userPromotionInviteRecordRO);
                promotionInviteRecordROS.add(userPromotionInviteRecordRO);
            }
        }
        return promotionInviteRecordROS;
    }

    @Override
    public UserPromotionInviteRecordRO getUserPromotionInviteRecordByInvitedUserCode(String invitedUserCode) {
        UserPromotionRecordEntity userPromotionRecordEntity = userPromotionRecordDao.queryUserPromotionByInvitedUserCode(invitedUserCode);
        UserPromotionInviteRecordRO userPromotionInviteRecordRO = null;
        if(userPromotionRecordEntity != null){
            userPromotionInviteRecordRO = new UserPromotionInviteRecordRO();
            BeanUtils.copyProperties(userPromotionRecordEntity, userPromotionInviteRecordRO);
        }
        return userPromotionInviteRecordRO;
    }

    @Override
    public boolean updateUserPromotionBonusAndNumber(String userCode, String invitedUserCode, Integer bonus) {
        userPromotionRecordDao.updateUserPromotionRecord(invitedUserCode, bonus);
        userPromotionMappingInfoDao.updateUserPromotionInfoByUserCode(userCode, null, bonus);
        return true;
    }

    @Override
    public int createPromotionHistoryFromTableInTask() {
        List<UserAccessEntity> userAccessEntities = userAccessDao.selectUserAccessForAllRealUser();
        int sum = 0;
        for(UserAccessEntity entity : userAccessEntities){
            UserPromotionMappingInfoEntity userPromotionMappingInfo = userPromotionMappingInfoDao.queryUserPromotionInfoByUserCode(entity.getUserCode());
            if(userPromotionMappingInfo != null){
                continue;
            }
            UserPromotionMappingInfoEntity userPromotionMappingInfoEntity = new UserPromotionMappingInfoEntity();
            userPromotionMappingInfoEntity.setUserId(entity.getId());
            userPromotionMappingInfoEntity.setUserCode(entity.getUserCode());
            userPromotionMappingInfoEntity.setLoginAccount(entity.getLoginNumber());
            userPromotionMappingInfoEntity.setInvitationCode(ShareCodeUtils.idToCode(entity.getId()));
            userPromotionMappingInfoEntity.setInvitationType(0);
            userPromotionMappingInfoEntity.setTotalBonus(0);
            userPromotionMappingInfoEntity.setTotalInvitePeople(0);
            userPromotionMappingInfoDao.insert(userPromotionMappingInfoEntity);
            sum = sum + 1;
        }
        return sum;
    }


    @Override
    public UserPromotionMappingInfo queryUserPromotionInfoByUserId(Long userId) {
        UserPromotionMappingInfoEntity userPromotionMappingInfoEntity = userPromotionMappingInfoDao.queryUserPromotionInfoByUserId(userId);
        UserPromotionMappingInfo userPromotionMappingInfo = null;
        if(userPromotionMappingInfoEntity != null){
            userPromotionMappingInfo = new UserPromotionMappingInfo();
            BeanUtils.copyProperties(userPromotionMappingInfoEntity, userPromotionMappingInfo);
        }
        return userPromotionMappingInfo;
    }
}
